﻿using System;
using System.Linq;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;
using Nop.Core.Plugins;
using Nop.Services.Configuration;
using Nop.Services.Discounts;
using Nop.Services.Localization;

namespace Nop.Plugin.DiscountRules.ReserveDiscount
{
    public partial class ReserveDiscountRequirementRule : BasePlugin, IDiscountRequirementRule
    {
        private readonly IDiscountService _discountService;
        public ReserveDiscountRequirementRule(IDiscountService _discountService)
        {
            this._discountService = _discountService;
        }
        public bool CheckRequirement(CheckDiscountRequirementRequest request)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            if (request.DiscountRequirement == null)
                throw new NopException("Discount requirement is not set");

            if (request.ProductVariant == null)
                return false;

            if (!request.ProductVariant.AllowedReserved)
                return false;
            if (request.Customer == null || request.Customer.IsGuest())
                return false;

            return request.ProductVariant.MinStockQuantity > request.ProductVariant.StockQuantity;
        }
        public string GetConfigurationUrl(int discountId, int? discountRequirementId)
        {
            string result = "Plugins/DiscountRulesRevert/Configure/?discountId=" + discountId;
            if (discountRequirementId.HasValue)
                result += string.Format("&discountRequirementId={0}", discountRequirementId.Value);
            return result;
        }
        public override void Install()
        {
            //locales
            this.AddOrUpdatePluginLocaleResource("Plugins.DiscountRules.Reserve.Fields.Amount", "Required spent amount");
            this.AddOrUpdatePluginLocaleResource("Plugins.DiscountRules.Reserve.Fields.Amount.Hint", "Discount will be applied if customer has spent/purchased x.xx amount.");
            base.Install();
        }

        public override void Uninstall()
        {
            //locales
            this.DeletePluginLocaleResource("Plugins.DiscountRules.ReserveDiscount.Fields.Amount");
            this.DeletePluginLocaleResource("Plugins.DiscountRules.ReserveDiscount.Fields.Amount.Hint");
            base.Uninstall();
        }

    }
}
